unit GMedPardPSNIrasai;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ActnList,
  GFixForm, bendri, Sandelys_dm, ExtCtrls, DBCtrls, Mask,
  cxLookAndFeelPainters, cxButtons, cxDBEdit,
  cxDropDownEdit, cxCalc, cxControls, cxContainer, cxEdit, cxTextEdit,
  cxMaskEdit, cxLookupEdit, cxDBLookupEdit, cxDBLookupComboBox, DB,
  cxPropertiesStore, Menus, cxGraphics;

type
  TfMedPardPSNIrasai = class(TGamaFixForm)
    eKodas: TcxDBLookupComboBox;
    btPaimtiDet: TcxButton;
    ActionList1: TActionList;
    aPaimtiPreke: TAction;
    eBKodas: TcxDBLookupComboBox;
    eGKodas: TcxDBLookupComboBox;
    ePavadinimas: TcxDBLookupComboBox;
    Label5: TLabel;
    eKiekis: TcxDBCalcEdit;
    eSandelis: TcxDBLookupComboBox;
    sMode: TStaticText;
    ePrekesID: TcxDBTextEdit;
    Label4: TLabel;
    eSN: TcxDBTextEdit;
    Panel1: TPanel;
    bCancel: TcxButton;
    bOK: TcxButton;
    sbPaimtiPreke: TcxButton;
    SpeedButton1: TcxButton;
    SpeedButton2: TcxButton;
    SpeedButton3: TcxButton;
    sbPaimtiSandeli: TcxButton;
    DS: TDataSource;
    GamaPropertiesStore: TcxPropertiesStore;
    procedure aPaimtiPrekeExecute(Sender: TObject);
    procedure sbPaimtiSandeliClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure eKiekisPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption;
      var Error: Boolean);
  private
    { Private declarations }
    procedure SetMode(v: string);
    procedure PriimtiPreke(Sender: TObject);
    procedure PriimtiSandeli(Sender: TObject);

  public
    { Public declarations }
    procedure ClearMode;
    property Mode: string write SetMode;
  end;

var
  fMedPardPSNIrasai: TfMedPardPSNIrasai;

implementation

uses
  Math, sandelys_conf,  doktip, uSandDokTip, Med_dm,
  tools;

{$R *.dfm}

procedure TfMedPardPSNIrasai.SetMode(v: string);
begin
  sMode.Caption := v;
end;

procedure TfMedPardPSNIrasai.ClearMode;
begin
  sMode.Caption := '';
end;

procedure TfMedPardPSNIrasai.aPaimtiPrekeExecute(Sender: TObject);
begin
  RegDokTipas[dtPrekes].Forma.Paimti(self.PriimtiPreke);
end;

procedure TfMedPardPSNIrasai.PriimtiPreke(Sender: TObject);
var
  SN: string;
begin
  with dmSandelys do begin
    if not (dmMed.tbPardIrasaiSN.State in [dsInsert, dsEdit]) then Exit;
    DS.DataSet['PrekesID'] := tbPrekes['ID'];
    DS.DataSet['Kiekis']   := tbPrekes.FieldByName('Likutis').AsFloat;

    if RegDokTipas[dtPrekes].Forma.ExValue[2] <> -1 then
      DS.DataSet['Sandelys'] := RegDokTipas[dtPrekes].Forma.ExValue[2];

    // yra duotas ir serijinis numeris
    SN := RegDokTipas[dtPrekes].Forma.ExValue[3];
    if Trim(SN) <> '' then begin
      DS.DataSet['SerNr'] := SN;
    end;
  end; // with dmSandelys do begin
  RegDokTipas[dtPrekes].Forma.Paimti(nil);
  ActiveControl := eKiekis;
end;

procedure TfMedPardPSNIrasai.sbPaimtiSandeliClick(Sender: TObject);
begin
  RegDokTipas[dtSandeliai].Forma.Paimti(self.PriimtiSandeli);
end;

procedure TfMedPardPSNIrasai.PriimtiSandeli(Sender: TObject);
begin
  with dmSandelys do begin
    if not (dmMed.tbPardIrasaiSN.State in [dsInsert, dsEdit]) then Exit;
    RegDokTipas[dtSandeliai].Forma.RefreshTables;
    DS.DataSet['Sandelys'] := RegDokTipas[dtSandeliai].Forma.ExValue;
  end; // with dmSandelys do begin
  RegDokTipas[dtSandeliai].Forma.Paimti(nil);
  ActiveControl := eSN;
end;

procedure TfMedPardPSNIrasai.FormShow(Sender: TObject);
begin
  DS.Enabled := true;
  ActiveControl := eKodas;
end;

procedure TfMedPardPSNIrasai.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  DS.Enabled := false;
end;

procedure TfMedPardPSNIrasai.FormCreate(Sender: TObject);
begin
  DS.Enabled := false;

  ePavadinimas.Width := self.ClientWidth - ePavadinimas.Left - 20;
  eSandelis.Width := self.ClientWidth - eSandelis.Left - 20;
  eSN.Width := self.ClientWidth - eSN.Left - 20;
  btPaimtiDet.Left := self.ClientWidth - btPaimtiDet.Width - 20;
  eKodas.Width := btPaimtiDet.Left - 8 - eKodas.Left;
  eBKodas.Width := eKodas.Width;
  eGKodas.Width := eKodas.Width;
end;

procedure TfMedPardPSNIrasai.eKiekisPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  if DisplayValue <> '' then
  DisplayValue := Rounding(DisplayValue, dmSandelys.SandConfigParam.KiekioTikslumas);
end;

end.
